package com.whfc.emp.service.impl;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.MathUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.*;
import com.whfc.emp.dto.*;
import com.whfc.emp.entity.*;
import com.whfc.emp.enums.AppAttachmentType;
import com.whfc.emp.manager.TrainBoxManager;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppTrainService;
import com.whfc.emp.service.AppTrainingCategoriesService;
import com.whfc.emp.service.AppTrainingQuestionsService;
import com.whfc.emp.service.AppTrainingService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysDictService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClasssName AppTrainServiceImpl
 * @Description 人员管理-培训
 * @Author hw
 * @Date 2020/11/26 16:18
 * @Version 1.0
 */
@DubboService(interfaceClass = AppTrainingService.class, version = "1.0.0", timeout = 5000)
public class AppTrainingServiceImpl implements AppTrainingService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppTrainingMapper appTrainingMapper;

    @Autowired
    private AppTrainingCategoriesService appTrainingCategoriesService;
    @Autowired
    private AppTrainingQuestionsService appTrainingQuestionService;


    @DubboReference(interfaceClass = SysDictService.class, version = "1.0.0")
    private SysDictService sysDictService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;


    @Override
    public PageData<AppTrainingDTO> list(AppTrainingListParam request) throws BizException {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppTrainingDTO> list = appTrainingMapper.selectByParam(request);
        if (list != null) {
            List<Integer> cates = list.stream().map(AppTrainingDTO::getCategoryId).collect(Collectors.toList());
            if (cates != null && cates.size() > 0) {
                Map<Integer, AppTrainingCategoryDTO> categoryMap = appTrainingCategoriesService.getCategoryMap(cates);
                if (categoryMap != null) {
                    list.forEach(item -> {
                        item.setCategoryName(categoryMap.get(item.getCategoryId()).getName());
                    });
                }
            }
        }
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    @Transactional
    public void add(AppTrainingAddParam request) throws BizException {
        AppTraining appTraining = new AppTraining();
        BeanUtils.copyProperties(request, appTraining);
        appTrainingMapper.insert(appTraining);

        //保存问题明细
        appTrainingQuestionService.saveBatchByTrainingId(request.getQuestions(), appTraining.getId());
    }

    @Override
    public void edit(AppTrainingEditParam request) throws BizException {
        AppTraining appTraining = new AppTraining();
        BeanUtils.copyProperties(request, appTraining);
        appTrainingMapper.updateByPrimaryKey(appTraining);
    }

    @Override
    public void del(Integer id) throws BizException {
        Integer count = appTrainingQuestionService.countByTrainingId(id);
        if (count > 0) {
            throw BizException.of(ResultEnum.PARAM_ERROR, "请先删除该培训下的所有问题");
        }
        appTrainingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageData<AppTrainingDTO> getTrainingList(AppTrainingListParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<AppTrainingDTO> list = appTrainingMapper.selectByParam(param);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }
}
